package com.vodone.v1pin.user.pm
{
	
	
	import com.vodone.v1pin.core.model.MainModelLocator;
	import com.vodone.v1pin.user.delegate.RegisterDelegate;
	import com.vodone.v1pin.user.delegate.UserBagDelegate;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	/**
	* 注册成功时派发 
	*/	
	[Event(name="registComplete")]
	/**
	 * 判断登陆是否成功时派发 
	 */	
	[Event(name="checkLoginComplete")]
	/**
	 * 验证码成功时派发 
	 */	
	[Event(name="codeSendComplete")]
	/**
	 * 激活成功时派发 
	 */	
	[Event(name="activeComplete")]
	/**
	* 请求失败时派发
	*/	
	[Event(name="fault")]
	/**
	* 开始请求时派发
	*/	
	[Event(name="startLoad")]
	
	public class RegisterPM extends EventDispatcher
	{
		public static const STARTLoad:String = "startLoad";
		public static const FAULT:String = "fault";
		
		public var object:Object;
		public var message:String;
		public function RegisterPM(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		/**
		 * 注册 
		 * @param userName
		 * @param password
		 * @param confirmPass
		 * @param email
		 * @param jz
		 * 
		 */		
		public function register( userName:String , password:String , confirmPass:String , email:String , jz:Boolean = false ):void
		{
			RegisterDelegate.register(resultHandler, "register" , userName , password , confirmPass , email , jz , faultHandler);
		}
		
		/**
		 * 登陆 
		 * @param userName
		 * @param password
		 * @param jz
		 * 
		 */		
		public function login( userName:String , password:String , jz:Boolean = false ):void
		{
			RegisterDelegate.register(resultHandler, "login" , userName , password , null , null , jz , faultHandler);
		}
		
		/**
		 * 登出
		 * @param userName
		 * @param password
		 * @param jz
		 * 
		 */		
		public function logout( userName:String ):void
		{
			RegisterDelegate.register(outHandler, "logout" , userName , "" , null , null , false , faultHandler);
		}
		
		private function outHandler( obj:Object ):void
		{
			MainModelLocator.getInstance().userLoginVO.userId = "";	
			MainModelLocator.getInstance().userLoginVO.userName = "";	
			MainModelLocator.getInstance().userLoginVO.isLogin = false;	
		}
		/**
		 * 检查是否登陆 
		 * 
		 */		
		public function checkLogin():void
		{
			RegisterDelegate.checkLogin(checkResultHandler, faultHandler);
		}
		
		/**
		 * 注册登陆返回 
		 * @param obj
		 * 
		 */		
		public function resultHandler( obj:Object ):void
		{
			if( obj && obj.hasOwnProperty("message") && obj.message.hasOwnProperty("code") && obj.message.code == 0 ){
				if( obj.hasOwnProperty("data") && obj.data is Array && obj.data.length > 0 ){
					object = obj.data[0];
				}
				this.dispatchEvent( new Event( "registComplete" ));
				return;
			}else if(  obj && obj.hasOwnProperty("message") && obj.message.hasOwnProperty("message") ){
				message = obj.message.message
				this.dispatchEvent( new Event( "fault" ));
				return;
			}
			this.dispatchEvent( new Event( "fault" ));
		}
		
		/**
		 * 检查是否登陆返回 
		 * @param obj
		 * 
		 */
		public var checkObj:Object;
		private function checkResultHandler( obj:Object ):void
		{
			checkObj = obj;
			this.dispatchEvent( new Event( "checkLoginComplete" ));
		}
		
		private function faultHandler( obj:Object ):void
		{
			this.dispatchEvent( new Event( "fault" ));
		}
		
		public function registerActive():void
		{
			RegisterDelegate.registerActive(activeResultHandler,activeFaultHandler);
		}
		
		public function activeResultHandler( obj:Object ):void
		{
			this.dispatchEvent( new Event( "activeComplete" ));
		}
		
		private function activeFaultHandler( obj:Object ):void
		{
			this.dispatchEvent( new Event( "fault" ));
		}
		
		public function getVerifyCode():void
		{
			RegisterDelegate.getVerifyCode(codeResultHandler,codeFaultHandler);
		}
		
		public function codeResultHandler( obj:Object ):void
		{
			this.dispatchEvent( new Event( "codeSendComplete" ));
		}
		
		private function codeFaultHandler( obj:Object ):void
		{
			this.dispatchEvent( new Event( "fault" ));
		}
	}
}